//
//  ManagerViewController.m
//  Leo01
//
//  Created by 张旭 on 15/9/2.
//  Copyright (c) 2015年 张旭. All rights reserved.
//
//#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
//#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
//
//#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
//#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
//#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
//#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
//
//#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
//#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
//#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
//#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)

#import "MainMemberManagerViewController.h"
#import "ManagerViewController.h"
#import "CollectionCell.h"
#import "AccountViewController.h"
#import "SetBabyInfoViewController.h"
#import "SafeAreaListViewController.h"
#import "RemoteShutDownViewController.h"
#import "ManageSetViewController.h"
#import "WatchMainDetailViewController.h"

#import "AppDelegate.h"
#import "LeoMsg.h"
#import "Watch.h"

@interface ManagerViewController ()
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *classNames;
@property (nonatomic, strong) NSArray *photoNames;
@property (nonatomic,retain) SyncManager *syncManager;
@property (nonatomic,retain) DataManager *dataManager;
@property (nonatomic,strong) Watch *selectWatch;
@end

@implementation ManagerViewController
@synthesize titles = _titles;
@synthesize collectionView = _collectionView;
@synthesize accountButton;
@synthesize babyInfoView;
@synthesize navBar;
@synthesize motherTabViewController;
@synthesize tabHeight;
@synthesize editButton;
@synthesize headImage;

float cellHeight;
float cellWidth;
float generalFontSize;//定义Cell平均文字大小
float generalY;//定义Cell平均y坐标
float generalLabelY;//定义CellLabel平均y坐标

-(void)dealloc
{
    self.dataMArr=NULL;
    self.titles=NULL;
    self.classNames=NULL;
    self.photoNames=NULL;
    self.syncManager=NULL;
    self.dataManager=NULL;
    self.selectWatch=NULL;
}
-(void)viewWillDisappear:(BOOL)animated
{
    if (motherTabViewController!=NULL)
    {
        motherTabViewController.navigationItem.rightBarButtonItem=NULL;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self registerObservers];
        //self.view.contentInset = UIEdgeInsetsMake(44,0,0,0);
    
    float height=[UIScreen mainScreen].bounds.size.height;
    tabHeight.constant=150*height/667;
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    
    
    self.automaticallyAdjustsScrollViewInsets = NO; 
    //[self.navigationController setNavigationBarHidden:YES];
    //self.navigationController=[[UINavigationController alloc]initWithRootViewController:self];
    [self initItemsName];
    [self initClassName];
    [self initPhotoNames];
    [self initActions];
    [self setUpCollection];
    [self setUpCollectionViewLayout];
    //[self makeNavButtonItemStyle];
    //导航栏返回按钮设置
//    UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
//    temporaryBarButtonItem.title =@"";
//    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
//    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
//    self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
    // Do any additional setup after loading the view from its nib.
   // [self initHeadImage];
    [self.headImage.layer setMasksToBounds:YES];
    self.headImage.layer.cornerRadius=self.headImage.bounds.size.width/2;
    NSLog(@"%f宽度",self.headImage.bounds.size.width);
    self.headImage.clipsToBounds=YES;
    self.syncManager=[[SyncManager alloc]init];
    self.dataManager=[[DataManager alloc]init];
    User *user=[User sharedSingleton];
    NSString *userId=user.uid;
    self.selectWatch=[self.dataManager getActiveWatch:userId];
}
-(void)initHeadImage
{
    //头像切圆代码
    self.headImage.layer.cornerRadius = headImage.bounds.size.width / 2;
    self.headImage.clipsToBounds = YES;
    NSLog(@"%f宽度",self.headImage.bounds.size.width);
    
    self.dataManager=[[DataManager alloc]init];
    User *user=[User sharedSingleton];
    self.selectWatch=[self.dataManager getActiveWatch:user.uid];

    if(self.selectWatch.pic_url!=NULL&&![self.selectWatch.pic_url isEqualToString:@"(null)"]&&![self.selectWatch.pic_url isEqualToString:@""])
    {
        self.headImage.image=[UIImage imageNamed:[[Tooler getDocumentDirectory] stringByAppendingString:self.selectWatch.pic_url]];
        //self.photoUrl=[[Tooler getDocumentDirectory] stringByAppendingString:watch.pic_url];
        //self.photoName=watch.photoName;
        //self.saveUrl=watch.pic_url;
    }
    else
    {
        if(self.selectWatch.gender==1)//女孩
        {
            self.headImage.image=[UIImage imageNamed:@"avater_girl.png"];
        }
        else//男孩
        {
            self.headImage.image=[UIImage imageNamed:@"avater_boy.png"];
        }
    }

}
- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
-(void)viewDidAppear:(BOOL)animated{
    if (motherTabViewController!=NULL)
    {
        motherTabViewController.navigationItem.rightBarButtonItem=accountButton;
    }
    //头像切圆代码
    self.headImage.layer.cornerRadius = headImage.bounds.size.width / 2;
    self.headImage.clipsToBounds = YES;
    
}
-(void)viewWillAppear:(BOOL)animated{

    [self initHeadImage];
    //[self initHeadImage];
    self.hidesBottomBarWhenPushed = NO;

    UIColor *blueBackgroundColor=[UIColor colorWithRed:71/255.0 green:178/255.0 blue:247/255.0 alpha:1];
    UIImage *blueBackgroundImage=[self imageWithColor:blueBackgroundColor];
    
    UINavigationBar *navigationBar = self.navigationController.navigationBar;
    // white.png图片自己下载个纯白色的色块，或者自己ps做一个
    [navigationBar setBackgroundImage:blueBackgroundImage
                       forBarPosition:UIBarPositionAny
                           barMetrics:UIBarMetricsDefault];
    [navigationBar setShadowImage:[UIImage new]];
    
    //同步数据
    //UIActivityIndicatorView *loadingView=[LeoMsg showLoadingView];
    //[self.view addSubview:loadingView];
    //[loadingView startAnimating];//Loading动画
    /*
    User *user=[User sharedSingleton];
    NSString *userId=user.uid;
    Watch *watch=[self.dataManager getActiveWatch:userId];
    NSString *watchId=watch.deviceUid;
    
    [self.syncManager getBoundUsers:watchId completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if(result==SUCESS)
        {
            [loadingView stopAnimating];
        }
        
    }];*///by yan
    [self.syncManager getGeoFences:self.selectWatch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        
    }];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //UITabBarItem *item = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMostRecent tag:1];
        UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:@"管理" image:[UIImage imageNamed:@"tb_setting_icon_normal@2x"] tag:2];
        [item setSelectedImage:[UIImage imageNamed:@"tb_setting_icon_hl@2x"]];
        self.tabBarItem = item;
        //self.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",9];
    }
    return self;
}

-(void)makeNavButtonItemStyle
{
    //
    //    float a=44;
    //    float b=48;
    //
    //    if ([UIScreen mainScreen].bounds.size.width ==375)
    //    {
    //        a=44;b=48;
    //    }
    //
    //    UIImage *leftImage=[UIImage imageNamed:@"set_normal"];
    //    leftImage=[self scaleImage:leftImage proportion:leftImage.size.height/a];
    //    [NavLeftButton setImageInsets:UIEdgeInsetsMake(0, -13, 0, 0)];
    //    [NavLeftButton setImage:leftImage];
    
    UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton addTarget:self action:@selector(jumpToAcountPage) forControlEvents:UIControlEventTouchDown];
    rightButton.frame = CGRectMake(0, 0, 44, 44);
    [rightButton setTitle:@"帐号" forState:UIControlStateNormal];
    UIBarButtonItem *backNavigationItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
    // 调整 leftBarButtonItem 在 iOS7 下面的位置
    if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                           target:nil action:nil];
        negativeSpacer.width = -15;
        self.navigationItem.rightBarButtonItems = @[negativeSpacer, backNavigationItem];
    }else
        self.navigationItem.rightBarButtonItem = backNavigationItem;
    
    
    
    UILabel *t = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    t.font = [UIFont systemFontOfSize:20];
    t.textColor = [UIColor whiteColor];
    t.textAlignment=NSTextAlignmentCenter;
    t.backgroundColor = [UIColor clearColor];
    t.text = @"设置";
    self.navigationItem.titleView = t;
}

- (void)initActions{
    //[accountButton addTarget:self action:@selector(jumpToAcountPage) forControlEvents:UIControlEventTouchUpInside];
    [accountButton setTarget:self];
    [accountButton setAction:@selector(jumpToAcountPage)];
    
    UITapGestureRecognizer *tapReconizerToSetBabyInfo = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toSetBabyInfo)];
    [self.babyInfoView addGestureRecognizer:tapReconizerToSetBabyInfo];
    
    [self.editButton addTarget:self action:@selector(toSetBabyInfo) forControlEvents:UIControlEventTouchDown];
    //self.editButton.adjustsImageWhenHighlighted=NO;
}

- (void)toSetBabyInfo{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    myDelegate.addWatchMethod=1;
    SetBabyInfoViewController *setBabyInfoViewController = [[SetBabyInfoViewController alloc]init];
    setBabyInfoViewController.type=2;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:setBabyInfoViewController animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    //[self presentViewController:setBabyInfoViewController animated:YES completion:nil];
}

- (void)jumpToAcountPage{
    AccountViewController *accountViewController=[[AccountViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:accountViewController animated:YES];
    self.hidesBottomBarWhenPushed = NO;
    //[self presentViewController:accountViewController animated:YES completion:nil];
}

- (void)initItemsName{
    NSArray *itemsName = [[NSArray alloc]initWithObjects:
    @"成员管理",
    @"联系人",
    @"安全区域",
    @"闹钟",
    @"免打扰时段",
    @"远程关机",
    @"历史通知",
    @"手表信息",
    @"设置", nil];
    self.titles = itemsName;
}

- (void)initPhotoNames{
    NSArray *photoNames = [[NSArray alloc]initWithObjects:
                           @"manage_icon_1",
                           @"manage_icon_2",
                           @"manage_icon_3",
                           @"manage_icon_4",
                           @"manage_icon_5",
                           @"manage_icon_6",
                           @"manage_icon_7",
                           @"manage_icon_8",
                           @"manage_icon_9", nil];
    
    self.photoNames = photoNames;
    
}
- (void)initClassName{
    NSArray *classNames = [[NSArray alloc]initWithObjects:
    @"MainMemberManagerViewController",
    @"MemberManagerViewController",
    @"SafeAreaListViewController",
    @"AlertViewController",
    @"SilentTimeListViewController",
    @"RemoteShutDownViewController",
    @"HistoryNotificationViewController",
    @"WatchMainDetailViewController",
    @"ManageSetViewController", nil];

    self.classNames = classNames;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)setUpCollection{
    self.dataMArr = [NSMutableArray array];
    for(NSInteger index = 0;index<self.titles.count; index++){
        //UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",(long)index+1]];
        //UIImage *image = [UIImage imageNamed:@"index_qq"];
        NSString *imageString=[self.photoNames objectAtIndex:index];
        UIImage *image=[UIImage imageNamed:imageString];
        
        //NSString *title = [NSString stringWithFormat:@"{0,%ld}",(long)index+1];
        NSString *title = [self.titles objectAtIndex:index];
        NSDictionary *dic = @{@"image": image, @"title":title};
        [self.dataMArr addObject:dic];
    }
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];
    self.collectionView.alwaysBounceVertical = YES;
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    
    
}

//设置collectionView的Layout
-(void)setUpCollectionViewLayout
{
    float width=[UIScreen mainScreen].bounds.size.width;
    float height=[UIScreen mainScreen].bounds.size.height;
    cellWidth=70*width/375; cellHeight=120*height/667;
    
    
    float hGip=(width-cellWidth*3-20)/6;
    float vGip=(height-20-44-150*height/667-cellHeight*3-20)/6;


    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(cellWidth, cellHeight);
    layout.minimumInteritemSpacing = 20;
    layout.minimumLineSpacing = 1; //上下的间距 可以设置0看下效果
    layout.sectionInset = UIEdgeInsetsMake(vGip, hGip, vGip, hGip);
    self.collectionView.collectionViewLayout=layout;

}

//每个section的item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataMArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *collectionCellID = @"CollectionCell";
    CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:collectionCellID forIndexPath:indexPath];
    
    NSDictionary *dic    = self.dataMArr[indexPath.row];
    UIImage *image       = dic[@"image"];
    NSString *title      = dic[@"title"];
    
    cell.imageView.image = image;
    cell.label.text = title;
    
    if (indexPath.row==4){
//        cell.label.adjustsFontSizeToFitWidth=NO;
//        cell.label.font = [UIFont systemFontOfSize:generalFontSize];
//        NSLog(@"%f",cell.label.frame.origin.x);
//        [cell.label setFrame:CGRectMake(cell.label.frame.origin.x-10, cell.label.frame.origin.y, cell.label.frame.size.width+20, cell.label.frame.size.width)];
//        [cell.label setBounds:CGRectMake(cell.label.frame.origin.x-10, cell.label.frame.origin.y, cell.label.frame.size.width+20, cell.label.frame.size.width)];
//
//        [cell.label layoutIfNeeded];
//        NSLog(@"%f",cell.label.frame.origin.x);
       // cell=[cell changeToNewCell];
        //cell.labelLeadingCon.constant=-10;
        //cell.labelTrailingCon.constant=10;
        float width=[UIScreen mainScreen].bounds.size.width;
        float height=[UIScreen mainScreen].bounds.size.height;
        cellWidth=70*width/375; cellHeight=120*height/667;
        float hGip=(width-cellWidth*3-20)/6;
        float vGip=(height-20-44-150*height/667-cellHeight*3-20)/6;
        
        if ([self.collectionView viewWithTag:999]==NULL){
            
            //***高度调整变量 kx,ky 根据IPhone尺寸调整
            int kx=0; int ky=0;
            if (width==375){kx=1;ky=6;}
            if (width==414){kx=0;ky=-1.5;}
            if (width==320){kx=8;ky=17;}

        cell.label.hidden=YES;
        UILabel *newLabel=[[UILabel alloc]initWithFrame:CGRectMake(cell.frame.origin.x-kx, generalLabelY+generalY-18-ky, cell.label.frame.size.width, cell.label.frame.size.height)];
            newLabel.font = [UIFont systemFontOfSize:generalFontSize];
            newLabel.textColor=[UIColor colorWithRed:67/255.0 green:67/255.0 blue:67/255.0 alpha:1.0];
            newLabel.text=@"免打扰时段";
            newLabel.tag=999;
            [self.collectionView addSubview:newLabel];
        }
    }
    else{
        if (indexPath.row==3){

            generalFontSize=cell.label.font.pointSize;
            generalY=cell.label.frame.origin.y;
            generalLabelY=cell.frame.origin.y;
        }
    }
    
    return cell;
};


//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    float edge=10;
//    float mistake=6;//误差
//    float width=[UIScreen mainScreen].bounds.size.width-10*2-mistake-edge*6;
//    //NSLog(@"%f",[UIScreen mainScreen].bounds.size.height);
//    float height=[UIScreen mainScreen].bounds.size.height-20-44-49-110-10*2-mistake;
//    //height=height/3;
//    return CGSizeMake(width/3, height/3);
    
//    if (indexPath.row==4){
//    
//    return CGSizeMake(cellWidth+5, cellHeight);
//    }
    
    return CGSizeMake(cellWidth, cellHeight);
    
//    float width=[UIScreen mainScreen].bounds.size.width;
//    float height=[UIScreen mainScreen].bounds.size.height;
//    cellWidth=(width-20-50)/3; cellHeight=(height-20+10)/3;
//    return CGSizeMake(cellWidth, cellHeight);

}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
//    float edge=10;
//    return UIEdgeInsetsMake(0, edge, 0, edge);
    float width=[UIScreen mainScreen].bounds.size.width;
    float height=[UIScreen mainScreen].bounds.size.height;
    float hGip=(width-cellWidth*3-20)/6;
    float vGip=(height-20-44-150*height/667-cellHeight*3-20)/6;
    NSLog(@"屏幕是  %f  %f",width,height);
    NSLog(@"间隙是  %f  %f",hGip,vGip);

    
//    if (section==4){
//        float f=5/2;
//        return UIEdgeInsetsMake(vGip, hGip-f,vGip,hGip-f);
//    }
    
    return UIEdgeInsetsMake(vGip, hGip,vGip,hGip);
//    float hGip=(cellWidth-70)/2;
//    float vGip=(cellHeight-120)/2;
//    return UIEdgeInsetsMake(vGip, hGip, vGip,hGip);
    
    
    
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"indexPath----%@,indexPath.row----%d",indexPath,indexPath.row);
    AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
    if(!mydelegate.isAdmin)
    {
        if (indexPath.row == [self.classNames indexOfObject:@"RemoteShutDownViewController"]||indexPath.row==[self.classNames indexOfObject:@"ManageSetViewController"])
        {
            [LeoMsg showMsg:@"您没有执行此项操作的权限"];
            return;
        }

    }
    else
    {
        if (indexPath.row == [self.classNames indexOfObject:@"RemoteShutDownViewController"]) {
            UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
            UIWindow *alertView=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
            RemoteShutDownViewController *remoteShutDown=[[RemoteShutDownViewController alloc]init];
            [remoteShutDown setMotherWindow:alertView];
            alertView.rootViewController=remoteShutDown;
            [keyWindow addSubview:alertView];
            [alertView setWindowLevel:UIWindowLevelAlert];
            [alertView makeKeyAndVisible];
            return;
        }
        
    }

    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSString *className = self.classNames[indexPath.row];
    UIViewController *subViewController = [[NSClassFromString(className) alloc]init];
    subViewController.title   = self.titles[indexPath.row];
    
    User *user = [User sharedSingleton];

    if (indexPath.row==[self.classNames indexOfObject:@"SafeAreaListViewController"]){
        Watch *selectWatch=[self.dataManager getActiveWatch:user.uid];
        if(self.syncManager!=NULL)
        {
            [self.syncManager getGeoFences:selectWatch.deviceUid completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
                if(result == SUCESS)
                {
                    self.hidesBottomBarWhenPushed = YES;
                    
                    [self.navigationController pushViewController:(UIViewController*)subViewController animated:YES];
                    self.hidesBottomBarWhenPushed = NO;
                    return ;
                }else{
                    [Tooler showHttpMsg:result description:description msg:@"获取安全区域数据失败，请检查网络"];
                }
            }];
        }

    }
    else{
        self.hidesBottomBarWhenPushed = YES;
    
        [self.navigationController pushViewController:(UIViewController*)subViewController animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }
}



//***注册通知模块,用于修改头像
-(void)registerObservers
{
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:CHANGEBABYINFO object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:UNBINDDEVICE object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:USERINFOUPDATE object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:CHANGEWATCH object:nil];
}
-(void)removeObservers
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CHANGEBABYINFO object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UNBINDDEVICE object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:USERINFOUPDATE object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:CHANGEWATCH object:nil];
}
- (void)receiveNotification:(NSNotification *)sender {
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
        return;
    User *user=[User sharedSingleton];
    if(user==NULL)
        return;

    if([sender.name isEqualToString:CHANGEBABYINFO]||[sender.name isEqualToString:USERINFOUPDATE])
    {
        [self initHeadImage];
        
    }
    else if([sender.name isEqualToString:UNBINDDEVICE]||[sender.name isEqualToString:CHANGEWATCH])
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self initHeadImage];
        });
    }
        
        
    
    
}



@end
